//
//  TFMedia.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFMedia: UIButton {
    @objc public let backgroundColorView = UIView()
    @objc public var audioModel:Bool = false

    @objc public var roleType:TalkfunRtcRole = .roleSpadmin
    @objc public let layerArray = NSMutableArray()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
      
        self.addSubview(self.backgroundColorView)
        //第0位置
        self.sendSubviewToBack(self.backgroundColorView)
        self.backgroundColorView.backgroundColor = UIColor.clear
        self.backgroundColorView.isHidden = true;
        self.layer.cornerRadius = 4
        self.backgroundColorView.layer.cornerRadius = 4
        self.backgroundColorView.clipsToBounds = true;
        
        self.backgroundColor = UIColor(red: 33/255.0, green: 38/255.0, blue: 46/255.0, alpha: 1)

        
        self.imageView!.backgroundColor = UIColor.clear
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        self.backgroundColorView.frame = self.bounds
        for i in 0..<self.layerArray.count {
         let labyer1 = self.layerArray[i] as! CALayer
        //i清空原来的背景色

            labyer1.removeFromSuperlayer()
           
        }
        self.layerArray.removeAllObjects()
        
        let gl = CAGradientLayer()
        gl.frame = self.backgroundColorView.bounds
        gl.startPoint = CGPoint(x: 0, y: 0)
        gl.endPoint = CGPoint(x: 1, y: 1)
        if(self.roleType == .roleSpadmin){
        
            gl.colors = [UIColor(red: 116/255.0, green: 224/255.0, blue: 255/255.0, alpha: 1.0).cgColor, UIColor(red: 100/255.0, green: 158/255.0, blue: 255/255.0, alpha: 1.0).cgColor]
           
            
        }else{
            gl.colors = [UIColor(red: 255/255.0, green: 214/255.0, blue: 116/255.0, alpha: 1.0).cgColor, UIColor(red: 255/255.0, green: 177/255.0, blue: 100/255.0, alpha: 1.0).cgColor]
          
        }
        gl.locations = [0,1]
        self.backgroundColorView.layer.addSublayer(gl)
        self.backgroundColorView.layer.cornerRadius = 3;
        self.layerArray.add(gl)
        
        let imageWidth = 45.5;
        
        let imageY = (self.frame.size.height - imageWidth)/2;
        let imageX = (self.frame.size.width - imageWidth)/2;
      
        self.imageView!.frame = CGRect(x: imageX, y: imageY, width: imageWidth, height: imageWidth)
       
    }


}
